﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QD.Tools;
using QD.LopPlat.UI.Code.DbContent;
using System.Security.Cryptography;
using System.Management;
using QD.LopPlat.UI.Model.Sys;

namespace QD.LopPlat.UI.Code
{
    public abstract class LicenseBase
    {
        public abstract string PrivateKey { get; set; }

        /// <summary>
        /// 软件授权验证是否通过
        /// <param name="databaseCode">组织名</param>
        /// <param name="pName">电脑名</param>
        /// </summary>
        public bool IsValidate(string pName)
        {
            string s_softId = GetSoftVersionByServcie();
            string f_softId = GetSoftVersionByDb(pName);
            return (s_softId == f_softId);
        }

        /// <summary>
        /// 软件有效期是否到期
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        public bool IsExceed(string pName)
        {
            DateTime? endDate = GetEndDateByDb(pName);
            if (endDate == null || Convert.ToDateTime(endDate) < Convert.ToDateTime(DateTime.Now.ToShortDateString()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据服务器信息获取软件序列号
        /// </summary>
        /// <returns></returns>
        public string GetSoftVersionByServcie()
        {
            //string softId = GetCpuID + GetHardDiskID;

            string softId = GetCpuID;

            return softId.ToMD5Encryption();
        }

        /// <summary>
        /// 根据数据库注册信息获取软件序列号
        /// </summary>
        /// <returns></returns>
        public abstract string GetSoftVersionByDb(string pName);

        /// <summary>
        /// 根据数据库注册信息获取软件截止日期
        /// </summary>
        /// <returns></returns>
        public abstract DateTime? GetEndDateByDb(string pName);

        /// <summary>
        /// 注册软件
        /// </summary>
        /// <param name="databaseCode">组织名</param>
        /// <param name="pName">主机名</param>
        /// <param name="licenseValue"></param>
        /// <returns></returns>
        public abstract void SetSoftVersion(string pName, string licenseValue);

        /// <summary>
        /// 注册有效期
        /// </summary>
        /// <param name="databaseCode">组织名</param>
        /// <param name="pName">主机名</param>
        /// <param name="licenseValue"></param>
        /// <returns></returns>
        public abstract void SetSoftEndDate(string pName, string licenseValue);

        /// <summary>
        /// 加密
        /// </summary>
        /// <returns></returns>
        public string Encryption(string publicKey, string encryptionStr)
        {
            RSACryptoServiceProvider Provider = new RSACryptoServiceProvider();
            Provider.FromXmlString(publicKey);

            byte[] bytes = new UnicodeEncoding().GetBytes(encryptionStr);

            byte[] encryptbytes = Provider.Encrypt(bytes, false);

            string str = Convert.ToBase64String(encryptbytes);
            return str;
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public string DecryptionRSA(string decryptionString)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(PrivateKey);

                byte[] decryptionByte = Convert.FromBase64String(decryptionString);
                byte[] aldecryptionBtype = rsa.Decrypt(decryptionByte, false);

                return new UnicodeEncoding().GetString(aldecryptionBtype);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //取CPU编号   
        private String GetCpuID
        {
            get
            {
                try
                {
                    ManagementClass mc = new ManagementClass("Win32_Processor");
                    ManagementObjectCollection moc = mc.GetInstances();

                    String strCpuID = null;
                    foreach (ManagementObject mo in moc)
                    {
                        strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                        break;
                    }
                    return strCpuID;
                }
                catch
                {
                    return "";
                }
            }
        }

        //取第一块硬盘编号   
        private static String GetHardDiskID
        {
            get
            {
                try
                {
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                    String strHardDiskID = null;
                    foreach (ManagementObject mo in searcher.Get())
                    {
                        strHardDiskID = mo["SerialNumber"].ToString().Trim();
                        break;
                    }
                    return strHardDiskID;
                }
                catch
                {
                    return "";
                }
            }
        }
    }
}
